//
//  NoticeGuide.m
//  louvre
//
//  Created by 李陆涛 on 2018/6/26.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "NoticeGuide.h"
#define BoardHeight (ISiPhone5 || ISiPhone4) ? 120 : 140

@interface NoticeGuide ()

@property (nonatomic) NSInteger index;

@end

@implementation NoticeGuide

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
    self.customBarStyle = UIStatusBarStyleLightContent;
    
    @weakify(self);
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        @strongify(self);
        if (self.index == 0) {
            [self hiddenVC];
            return ;
        }
        
        BOOL hasSpeed = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasSpeed"];
        BOOL hasNodeSearch = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasNodeSearch"];
        
        if (self.index == 1 && !hasSpeed) {
            [self.view removeAllSubviews];
            [self setupPlayerSpeed];
        }else if(self.index == 1 && !hasNodeSearch){
            [self.view removeAllSubviews];
            [self setupSearchGuide];
        }
        
        if (self.index == 2) {
            [self.view removeAllSubviews];
            [self setupSearchGuide];
        }
        
        self.index --;
    }]];
}

+ (void)showHomeWithVC:(UIViewController *)vc {
    
    BOOL hasInHome = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasInHome"];
    
    if (hasInHome) {
        return;
    }
    
    NoticeGuide *noticeVC = [NoticeGuide new];
    
    noticeVC.index = 0;
    [noticeVC setupHome];
    [noticeVC showWithVC:vc];
    
}

+(void)showSearchGuideView:(UIViewController *)vc {
    BOOL hasNodeSearch = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasNodeSearch"];
    if (hasNodeSearch) {
        return;
    }
    NoticeGuide *noticeVC = [NoticeGuide new];
    
    noticeVC.index = 0;
    [noticeVC setupSearchGuide];
    [noticeVC showWithVC:vc];
    
}
+ (void)showPlayerWithMenu:(NSArray *)menuTagsArray vc:(UIViewController *)vc {
    
    NSMutableArray *tagArray = [NSMutableArray array];
    
    for (NSNumber *type in menuTagsArray) {
        
        switch (type.integerValue) {
            case MenuTypeQuestion: {
                
                BOOL hasQuestion = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasQuestion"];
                
                if (!hasQuestion) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
            case MenuTypeComment: {
                
                BOOL hasComment = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasComment"];
                
                if (!hasComment) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
            case MenuTypeMap: {
                
                BOOL hasMap = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasMap"];
                
                if (!hasMap) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
            case MenuTypeCamera: {
                
                BOOL hasCamera = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasCamera"];
                
                if (!hasCamera) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
            case MenuTypeSearch: {
                
                BOOL hasSearch = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasSearch"];
                
                if (!hasSearch) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
                
            case MenuTypeKeyboard: {
                
                BOOL hasJianpan = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasJianpan"];
                
                if (!hasJianpan) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
                
            case MenuTypeList: {
                
                BOOL hasLiebiao = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasLiebiao"];
                
                if (!hasLiebiao) {
                    
                    [tagArray addObject:type];
                }
            }
                break;
            default:
                break;
        }
    }
    
    BOOL hasSpeed = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasSpeed"];
    
    BOOL hasNodeSearch = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasNodeSearch"];
    
    if (tagArray.count == 0 && hasSpeed) {
        
        if (hasNodeSearch) {
            return;
        } else if (![menuTagsArray containsObject:@(MenuTypeKeyboard)] && ![menuTagsArray containsObject:@(MenuTypeList)]) {
            
            return;
        }
    }
    
    
    
    
    NoticeGuide *noticeVC = [NoticeGuide new];
    
    if (!hasSpeed && tagArray.count > 0) {
        
        if ([menuTagsArray containsObject:@(MenuTypeKeyboard)] || [menuTagsArray containsObject:@(MenuTypeList)]) {
            noticeVC.index = 2;
        } else {
            
            noticeVC.index = 1;
        }
        
    } else if(!hasNodeSearch && ([menuTagsArray containsObject:@(MenuTypeKeyboard)] || [menuTagsArray containsObject:@(MenuTypeList)])){
        noticeVC.index = 1;
        
    }else{
        noticeVC.index = 0;
    }
    
    if (tagArray.count > 0) {
        
        CGFloat boardHeight = ((ISiPhone5 || ISiPhone4) ? 120 : 140);
        
        if (![menuTagsArray containsObject:@(MenuTypeKeyboard)] && ![menuTagsArray containsObject:@(MenuTypeList)]) {
            
            boardHeight = 140;
        }
        
        
        CGFloat cTop = kScreenHeight - kBottomHeight - (98 + boardHeight + 48);
        
        if ([tagArray containsObject:@(MenuTypeKeyboard)]) {
            
            cTop += boardHeight;
        }
        
        [noticeVC setupPlayerWithMenu:menuTagsArray tagArray:tagArray menuViewTop:cTop];
    } else if (!hasSpeed) {
        
        [noticeVC setupPlayerSpeed];
    }else if(!hasNodeSearch){
        [noticeVC setupSearchGuide];
    }
    
    [noticeVC showWithVC:vc];

}

- (void)showWithVC:(UIViewController *)vc {
    
    self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    
    [vc presentViewController:self animated:NO completion:nil];
}

- (void)hiddenVC {
    
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)setupHome {
    
    UIImageView *topImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"上下切换讲解点", nil)]];//162 × 90
    
    topImageView.frame = CGRectMake((kScreenWidth - 162) / 2, kNavigationBarHeight + 60, 162, 90);
    
    [self.view addSubview:topImageView];

    
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"左滑查看详情", nil)]];// 160 87

    leftImageView.frame = CGRectMake((kScreenWidth - 160) - 20, (kScreenHeight - 87 - kBottomHeight) * 2 / 3, 160, 87);
    [self.view addSubview:leftImageView];
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasInHome"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)setupPlayerWithMenu:(NSArray *)menuTagsArray tagArray:(NSArray *)tagArray menuViewTop:(CGFloat)top {
    
    CGFloat width = kScreenWidth / menuTagsArray.count;
    
    for (int i = 0; i< menuTagsArray.count; i ++) {
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * width, top, width, 48);

        btn.userInteractionEnabled = NO;
        [self.view addSubview:btn];
        
        NSNumber * type = menuTagsArray[i];
        
        if (![tagArray containsObject:type]) {
            continue;
        }
        
        switch (type.integerValue) {
            case MenuTypeQuestion:
            {
                [btn setImage:[[UIImage imageNamed:@"question-grey"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_ASK2", nil)]];//73 × 74
                
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + 20, top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasQuestion"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
            }
                break;
            case MenuTypeComment:
            {
                [btn setImage:[[UIImage imageNamed:@"comment-grey"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_COMMENT", nil)]];//36 × 40
                
                imageView.clipsToBounds = NO;
                imageView.contentMode = UIViewContentModeCenter;
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + width / 2 - size.width /2 , top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasComment"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
                break;
            case MenuTypeMap:
            {
                [btn setImage:[[UIImage imageNamed:@"map-grey"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_MAP", nil)]];//70 × 66
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + width / 2 - size.width/2, top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasMap"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
                break;
            case MenuTypeCamera:
            {
                [btn setImage:[[UIImage imageNamed:@"camera-grey"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_TAKEPHOTO", nil)]];//36 × 39
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + width / 2 - 30, top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasCamera"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
                break;
            case MenuTypeSearch:
            {
                [btn setImage:[[UIImage imageNamed:@"search-grey"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_SEARCHALL", nil)]];//73 × 68
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + width / 2 - size.width , top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSearch"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
                break;
            case MenuTypeList:
            {
                
                [btn setImage:[[UIImage imageNamed:@"列表"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_ALL2", nil)]];//54 × 90
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + width / 2 - size.width , top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasLiebiao"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
                break;
                
                
            case MenuTypeKeyboard:
            {
                
                [btn setImage:[[UIImage imageNamed:@"键盘"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
                
                UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_SEARCHID", nil)]];//71 × 91
                CGSize size = imageView.size;
                imageView.frame = CGRectMake(i * width + width / 2 - size.width , top - size.height, size.width, size.height);
                [self.view addSubview:imageView];
                
                [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasJianpan"];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
                break;
            default:
                break;
        }
    }
}

- (void)setupPlayerSpeed {
    
    
    CGFloat top = kScreenHeight - kBottomHeight - 90 + 38.5;
    
    UIButton *speedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    speedBtn.frame = CGRectMake(15.5, top, 36, 36);
    speedBtn.userInteractionEnabled = NO;
    [self.view addSubview:speedBtn];
    [speedBtn setImage:[UIImage imageNamed:@"speed1.0x"] forState:UIControlStateNormal];
    
    UIImageView *speedimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_SPEED", nil)]];//110 × 69
    CGSize size = speedimageView.size;
    
    speedimageView.frame = CGRectMake(16 , top - size.height - 5, size.width, size.height);
    [self.view addSubview:speedimageView];
    
    
    UIButton *sequentialBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sequentialBtn.userInteractionEnabled = NO;
    sequentialBtn.frame = CGRectMake(59.5, top, 36, 36);
    [self.view addSubview:sequentialBtn];
    [sequentialBtn setImage:[UIImage imageNamed:@"连续播放"] forState:UIControlStateNormal];
    
    UIImageView *sequentimageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_ALLPLAY", nil)]];//128 × 56
    CGSize size2 = sequentimageView.size;
    
    sequentimageView.frame = CGRectMake(60 + 36 + 5 , top - size2.height + 18, size2.width, size2.height);
    [self.view addSubview:sequentimageView];
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasSpeed"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}


- (void)setupSearchGuide{
    
    CGFloat width = kScreenWidth - 40;
    CGFloat height = width/335 * 232;
    CGFloat yy = kScreenHeight - 146 - (BoardHeight) - height;
    if (ISiPhoneX) {
        yy = kScreenHeight - 180 - (BoardHeight) - height;
    }
    UIImageView * searchGuideImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20,yy, width,height)];
    searchGuideImageView.image = [UIImage imageNamed:MYLocalizedString(@"GUIDE_PLAYER_SEARCHGUIDE", nil)];
    [self.view addSubview:searchGuideImageView];
    
    CGFloat  Y = kScreenHeight - 98 - (BoardHeight);
    if (ISiPhoneX) {
        Y = kScreenHeight - 98 - (BoardHeight) - 34;
    }
    UIView * contentView = [[UIView alloc] initWithFrame:CGRectMake(0,Y , kScreenWidth, BoardHeight)];
    [self.view addSubview:contentView];
        [self initKeyBoardView:contentView];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasNodeSearch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)initKeyBoardView:(UIView *)view {
    
    NSArray * arr = @[@"1",@"2",@"3",@"0",@"4",@"5",@"6",@"删除",@"7",@"8",@"9"];//,@"清空"
    
    CGFloat Hspace = 16;
    CGFloat Vspace = 11;
    CGFloat width = (kScreenWidth - 80)/4;
    CGFloat Height =((BoardHeight) - 44)/3;
    CGFloat Y = Vspace;
    for (NSInteger i = 0; i < arr.count; i++) {
        
        if (i == 4) {
            Y = Vspace * 2 + Height;
        }else if(i == 8){
            Y = Vspace * 3 + Height * 2;
        }
        NSInteger num = i%4;
        NSString * str = arr[i];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(Hspace + num *(Hspace + width), Y, width, Height);
        btn.userInteractionEnabled = NO;
        btn.backgroundColor = [UIColor colorWithRed:220/255.0 green:223/255.0 blue:228/255.0 alpha:0.4];
        
        if ([str isEqualToString:@"删除"]) {
            [btn setImage:[UIImage imageNamed:@"deletekey"] forState:UIControlStateNormal];
            btn.frame = CGRectMake(Hspace + num *(Hspace + width), Y, width, 2 * Height + Vspace);
        }else {
            [btn setTitle:str forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        [view addSubview:btn];
    }
    
}
@end
